-- @author Dozingfiretruck
-- @license MIT
-- @release 2021.4.29

module(...,package.seeall)

local LCD_WIDTH, LCD_HEIGHT, LCD_BPP = disp.getlcdinfo()


--0:墙外 1:墙 2:墙内空地 3:箱子 4:箱子要去的目标 5:人 6:箱子与目标重合 7:目标与人重合
local checkpoint_1 = {
    {0,0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,1,1,1,0,0,0,0,0},
    {0,0,0,0,1,4,1,0,0,0,0,0},
    {0,0,0,0,1,2,1,1,1,1,0,0},
    {0,0,1,1,1,3,2,3,4,1,0,0},
    {0,0,1,4,2,3,5,1,1,1,0,0},
    {0,0,1,1,1,1,3,1,0,0,0,0},
    {0,0,0,0,0,1,4,1,0,0,0,0},
    {0,0,0,0,0,1,1,1,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0}
    }

local checkpoint_2 = {
    {1,1,1,1,1,0,0,0,0,0,0,0},
    {1,2,2,2,1,0,0,0,0,0,0,0},
    {1,2,3,2,1,0,1,1,1,1,1,1},
    {1,2,3,5,1,0,1,2,2,2,4,1},
    {1,1,1,3,1,1,1,2,2,2,4,1},
    {1,1,1,2,2,2,2,2,2,2,4,1},
    {0,1,2,2,2,1,2,2,2,2,2,1},
    {0,1,2,2,2,1,2,2,2,2,2,1},
    {0,1,2,2,2,1,1,1,1,1,1,1},
    {0,1,1,1,1,1,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0}
    }
local map_table = {checkpoint_1,checkpoint_2}

local map = 1
local checkpoint
local location_guy
local game_data = {}

local function draw_map()
    disp.setbkcolor(0xf6e7b3)--f6e7b3
    disp.clear()
    disp.puttext(common.utf8ToGb2312("关卡:"),10,2)
    disp.puttext(common.utf8ToGb2312(map),55,2)
    for i = 1, 11 do
        for j = 1, 12 do
            local location = checkpoint[i][j]
            if location == 0 or location == 2 then
            elseif location == 1 then
                disp.putimage("/lua/wall.png",j*20-20,i*20)
            elseif location == 3 or location == 6 then
                disp.putimage("/lua/box.png",j*20-20,i*20)
            elseif location == 4 then
                disp.putimage("/lua/target.png",j*20-20,i*20)
            elseif location == 5 then
                location_guy = {i,j}
                disp.putimage("/lua/guy.png",j*20-20,i*20)
            elseif location == 7 then
                location_guy = {i,j}
                disp.putimage("/lua/target.png",j*20-20,i*20)
                disp.putimage("/lua/guy.png",j*20-20,i*20)
            end
        end
    end
    disp.update()
end

local function chek_over()
    for i = 1, 11 do
        for j = 1, 12 do
            local location = checkpoint[i][j]
            if location == 3 then
                return 0
            end
        end
    end
    return -1
end

local function game_thread()
    draw_map()
    --audio.play(0,"FILE","/lua/bgm.mp3",1,nil,true)
    while true do
        local result, data = sys.waitUntil("key",20)
        if result == true then
            if data == "key_left" then
                if checkpoint[location_guy[1]][location_guy[2]-1]==2 then--下个位置是空地
                    checkpoint[location_guy[1]][location_guy[2]-1]=5--下个位置变成人
                    if checkpoint[location_guy[1]][location_guy[2]]==5 then--当前位置是人
                        checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
                    elseif checkpoint[location_guy[1]][location_guy[2]]==7 then--当前位置是目标与人重合
                        checkpoint[location_guy[1]][location_guy[2]]=4--当前位置变成目标
                    end
                elseif checkpoint[location_guy[1]][location_guy[2]-1]==3 then--下个位置是箱子
                    if checkpoint[location_guy[1]][location_guy[2]-2]==2 then--箱子后是空地
                        checkpoint[location_guy[1]][location_guy[2]-1]=5--下个位置变成人
                        checkpoint[location_guy[1]][location_guy[2]-2]=3--箱子后的位置变为箱子
                        checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
                    elseif checkpoint[location_guy[1]][location_guy[2]-2]==4 then--箱子后是目标
                        checkpoint[location_guy[1]][location_guy[2]-1]=5--下个位置变成人
                        checkpoint[location_guy[1]][location_guy[2]-2]=6--箱子后的位置变为箱子与目标重合
                        checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
                    end
                elseif checkpoint[location_guy[1]][location_guy[2]-1]==4 then--下个位置是目标
                    checkpoint[location_guy[1]][location_guy[2]-1]=7--下个位置变成目标与人重合
                    if checkpoint[location_guy[1]][location_guy[2]]==5 then--当前位置是人
                        checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
                    elseif checkpoint[location_guy[1]][location_guy[2]]==7 then--当前位置是目标与人重合
                        checkpoint[location_guy[1]][location_guy[2]]=4--当前位置变成目标
                    end
                elseif checkpoint[location_guy[1]][location_guy[2]-1]==6 then--下个位置是箱子与目标重合
                    if checkpoint[location_guy[1]][location_guy[2]-2]==2 then--箱子后是空地
                        checkpoint[location_guy[1]][location_guy[2]-1]=7--下个位置变成目标与人重合
                        checkpoint[location_guy[1]][location_guy[2]-2]=3--箱子后的位置变为箱子
                        if checkpoint[location_guy[1]][location_guy[2]]==5 then--当前位置是人
                            checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
                        elseif checkpoint[location_guy[1]][location_guy[2]]==7 then--当前位置是目标与人重合
                            checkpoint[location_guy[1]][location_guy[2]]=4--当前位置变成目标
                        end
                    elseif checkpoint[location_guy[1]][location_guy[2]-2]==4 then--箱子后是目标
                        checkpoint[location_guy[1]][location_guy[2]-1]=7--下个位置变成目标与人重合
                        checkpoint[location_guy[1]][location_guy[2]-2]=6--箱子后的位置变为箱子与目标重合
                        if checkpoint[location_guy[1]][location_guy[2]]==5 then--当前位置是人
                            checkpoint[location_guy[1]][location_guy[2]]=2--当前位置变成空地
                        elseif checkpoint[location_guy[1]][location_guy[2]]==7 then--当前位置是目标与人重合
                            checkpoint[location_guy[1]][location_guy[2]]=4--当前位置变成目标
                        end
                    end
                end
            elseif data == "key_right" then
                if checkpoint[location_guy[1]][location_guy[2]+1]==2 then
                    checkpoint[location_guy[1]][location_guy[2]+1]=5
                    if checkpoint[location_guy[1]][location_guy[2]]==5 then
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                        checkpoint[location_guy[1]][location_guy[2]]=4
                    end
                elseif checkpoint[location_guy[1]][location_guy[2]+1]==3 then
                    if checkpoint[location_guy[1]][location_guy[2]+2]==2 then
                        checkpoint[location_guy[1]][location_guy[2]+1]=5
                        checkpoint[location_guy[1]][location_guy[2]+2]=3
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]][location_guy[2]+2]==4 then
                        checkpoint[location_guy[1]][location_guy[2]+1]=5
                        checkpoint[location_guy[1]][location_guy[2]+2]=6
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    end
                elseif checkpoint[location_guy[1]][location_guy[2]+1]==4 then
                    checkpoint[location_guy[1]][location_guy[2]+1]=7
                    if checkpoint[location_guy[1]][location_guy[2]]==5 then
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                        checkpoint[location_guy[1]][location_guy[2]]=4
                    end
                elseif checkpoint[location_guy[1]][location_guy[2]+1]==6 then
                    if checkpoint[location_guy[1]][location_guy[2]+2]==2 then
                        checkpoint[location_guy[1]][location_guy[2]+1]=7
                        checkpoint[location_guy[1]][location_guy[2]+2]=3
                        if checkpoint[location_guy[1]][location_guy[2]]==5 then
                            checkpoint[location_guy[1]][location_guy[2]]=2
                        elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                            checkpoint[location_guy[1]][location_guy[2]]=4
                        end
                    elseif checkpoint[location_guy[1]][location_guy[2]+2]==4 then
                        checkpoint[location_guy[1]][location_guy[2]+1]=7
                        checkpoint[location_guy[1]][location_guy[2]+2]=6
                        if checkpoint[location_guy[1]][location_guy[2]]==5 then
                            checkpoint[location_guy[1]][location_guy[2]]=2
                        elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                            checkpoint[location_guy[1]][location_guy[2]]=4
                        end
                    end
                end
            elseif data == "key_up" then
                if checkpoint[location_guy[1]-1][location_guy[2]]==2 then
                    checkpoint[location_guy[1]-1][location_guy[2]]=5
                    if checkpoint[location_guy[1]][location_guy[2]]==5 then
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                        checkpoint[location_guy[1]][location_guy[2]]=4
                    end
                elseif checkpoint[location_guy[1]-1][location_guy[2]]==3 then
                    if checkpoint[location_guy[1]-2][location_guy[2]]==2 then
                        checkpoint[location_guy[1]-1][location_guy[2]]=5
                        checkpoint[location_guy[1]-2][location_guy[2]]=3
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]-2][location_guy[2]]==4 then
                        checkpoint[location_guy[1]-1][location_guy[2]]=5
                        checkpoint[location_guy[1]-2][location_guy[2]]=6
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    end
                elseif checkpoint[location_guy[1]-1][location_guy[2]]==4 then
                    checkpoint[location_guy[1]-1][location_guy[2]]=7
                    if checkpoint[location_guy[1]][location_guy[2]]==5 then
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                        checkpoint[location_guy[1]][location_guy[2]]=4
                    end
                elseif checkpoint[location_guy[1]-1][location_guy[2]]==6 then
                    if checkpoint[location_guy[1]-2][location_guy[2]]==2 then
                        checkpoint[location_guy[1]-1][location_guy[2]]=7
                        checkpoint[location_guy[1]-2][location_guy[2]]=3
                        if checkpoint[location_guy[1]][location_guy[2]]==5 then
                            checkpoint[location_guy[1]][location_guy[2]]=2
                        elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                            checkpoint[location_guy[1]][location_guy[2]]=4
                        end
                    elseif checkpoint[location_guy[1]-2][location_guy[2]]==4 then
                        checkpoint[location_guy[1]-1][location_guy[2]]=7
                        checkpoint[location_guy[1]-2][location_guy[2]]=6
                        if checkpoint[location_guy[1]][location_guy[2]]==5 then
                            checkpoint[location_guy[1]][location_guy[2]]=2
                        elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                            checkpoint[location_guy[1]][location_guy[2]]=4
                        end
                    end
                end
            elseif data == "key_down" then
                if checkpoint[location_guy[1]+1][location_guy[2]]==2 then
                    checkpoint[location_guy[1]+1][location_guy[2]]=5
                    if checkpoint[location_guy[1]][location_guy[2]]==5 then
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                        checkpoint[location_guy[1]][location_guy[2]]=4
                    end
                elseif checkpoint[location_guy[1]+1][location_guy[2]]==3 then
                    if checkpoint[location_guy[1]+2][location_guy[2]]==2 then
                        checkpoint[location_guy[1]+1][location_guy[2]]=5
                        checkpoint[location_guy[1]+2][location_guy[2]]=3
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]+2][location_guy[2]]==4 then
                        checkpoint[location_guy[1]+1][location_guy[2]]=5
                        checkpoint[location_guy[1]+2][location_guy[2]]=6
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    end
                elseif checkpoint[location_guy[1]+1][location_guy[2]]==4 then
                    checkpoint[location_guy[1]+1][location_guy[2]]=7
                    if checkpoint[location_guy[1]][location_guy[2]]==5 then
                        checkpoint[location_guy[1]][location_guy[2]]=2
                    elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                        checkpoint[location_guy[1]][location_guy[2]]=4
                    end
                elseif checkpoint[location_guy[1]+1][location_guy[2]]==6 then
                    if checkpoint[location_guy[1]+2][location_guy[2]]==2 then
                        checkpoint[location_guy[1]+1][location_guy[2]]=7
                        checkpoint[location_guy[1]+2][location_guy[2]]=3
                        if checkpoint[location_guy[1]][location_guy[2]]==5 then
                            checkpoint[location_guy[1]][location_guy[2]]=2
                        elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                            checkpoint[location_guy[1]][location_guy[2]]=4
                        end
                    elseif checkpoint[location_guy[1]+2][location_guy[2]]==4 then
                        checkpoint[location_guy[1]+1][location_guy[2]]=7
                        checkpoint[location_guy[1]+2][location_guy[2]]=6
                        if checkpoint[location_guy[1]][location_guy[2]]==5 then
                            checkpoint[location_guy[1]][location_guy[2]]=2
                        elseif checkpoint[location_guy[1]][location_guy[2]]==7 then
                            checkpoint[location_guy[1]][location_guy[2]]=4
                        end
                    end
                end

            elseif data == "key_back" then
                game_data = json.decode(io.readFile("/box_game"))
                checkpoint = game_data[map]
                draw_map()
            elseif data == "key_ok" then
                disp.putimage("/lua/start.png",40,100)
                disp.update()
                while true do
                    local result, data = sys.waitUntil("key",20)
                    if result == true then
                        if data == "key_ok" then
                            break
                        end
                    end
                end
            end
            draw_map()
            if chek_over() == -1 then
                map = map+1
                if map>#map_table then
                    map = 1
                    game_data = json.decode(io.readFile("/box_game"))
                    checkpoint = game_data[map]
                    draw_map()
                else
                    checkpoint = {}
                    checkpoint = game_data[map]
                    draw_map()
                end
            end
        end
    end
end

local function game_init()
    disp.setbkcolor(0x0000)
    disp.clear()
    lcd.setcolor(0x00FF)
    disp.setfontheight(24)
    disp.puttext(common.utf8ToGb2312("推箱子"),(lcd.HEIGHT-string.utf8Len("推箱子")*24)/2,170)
    disp.putimage("/lua/box_game.png",90,90)
    disp.update()
    disp.setfontheight(16)
    sys.wait(2000)
    io.writeFile("/box_game", json.encode(map_table))
    game_data = json.decode(io.readFile("/box_game"))
    checkpoint = game_data[map]
    draw_map()
    disp.putimage("/lua/start.png",40,100)
    disp.update()
    while true do
        local result, data = sys.waitUntil("key",20)
        if result == true then
            if data == "key_left" then

            elseif data == "key_right" then

            elseif data == "key_up" then

            elseif data == "key_down" then

            elseif data == "key_ok" then
                game_thread()
            end
        end
    end
end

sys.taskInit(game_init)

